"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.
"""

"""
Case Type   : 资源池化
Case Name   : 资源池化场景下，跑tpcc时, 查询gs_lwlock_status信息
Create At   : 2024-11-22
Owner       : huzhengchao
Description :
    1、查询集群状态
    2、启动tpcc
    3、查看参数 track_activities
    4、查看 gs_lwlock_status()的信息
    5、关闭参数 track_activities
    6、重启集群
    7、启动tpcc
    8、查看参数 track_activities
    9、查看 gs_lwlock_status()的信息
    10、打开参数 track_activities
    11、恢复集群原始状态

Expect:
    1、查询结果正常
    2、tpcc启动成功
    3、参数为on
    4、查询得到lwlock信息
    5、关闭参数成功
    6、重启集群成功
    7、启动tpcc成功
    2、参数打开
    4、能够查询到
    5、关闭参数成功
    6、重启集群成功，状态正常，数据库节点状态正常
    7、参数都关闭
    8、不能够看到相关的线程信息
    9、查询报告警，没有数据
    10、打开成功
    11、恢复集群原始状态成功,集群状态正常
History     :
    1、2024-11-22 创建
"""
import os
import time
import unittest

from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.ComThread import ComThread
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class SharedStorage(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.com = Common()
        self.tpcc_user = Node('PrimaryRoot')
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.s1_comsh = CommonSH('Standby1DbUser')

    def test_shared_storage(self):
        text = '-----step1:查询数据库集群状态-----'
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)

        text = '-----step2:启动tpcc;-----'
        self.log.info(text)
        start_tpcc = ComThread(self.com.start_tpcc,
                               args=(self.tpcc_user,'/data/benchmarksql-5.0/run',
                               './runBenchmark.sh props_write.pg'))
        start_tpcc.setDaemon(True)
        start_tpcc.start()
        
        text = '-----step3:查看参数 track_activities; expect:参数都打开-----'
        sql = f"show track_activities;"
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('on', status, '执行失败' + text)
        
        text = '-----step4:查询gs_lwlock_status; expect:数据不为0-----'
        self.log.info(text)
        time.sleep(20)
        sql = "select case when count(*) > 0 then 'yes' else 'no' end as have_info " + \
              "from gs_lwlock_status(); "
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('yes', status, '执行失败：查询不到lwlock锁的信息。' + text)
        
        text = '-----step5: 关闭参数 expect:关闭成功-----'
        result = Primary_SH.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'track_activities=off')
        self.assertTrue(result, '执行失败' + text)
        
        text = '-----step6:重启集群 expect:启动集群成功，集群状态正常，数据库节点状态正常-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='stop')
        self.log.info(status)
        self.assertIn('cm_ctl: stop cluster successfully.', status, '执行失败' + text)
        status = Primary_SH.exec_cm_ctl(mode='start')
        self.log.info(status)
        self.assertIn('cm_ctl: start cluster successfully.', status, '执行失败' + text)
        status = Primary_SH.exec_cm_ctl(mode='switchover', param='-a')
        self.log.info(status)
        self.assertIn(self.constant.cm_switchover_success_msg, status, '执行失败' + text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)
        
        text = '-----step7:启动tpcc;-----'
        self.log.info(text)
        start_tpcc = ComThread(self.com.start_tpcc,
                               args=(self.tpcc_user,'/data/benchmarksql-5.0/run',
                               './runBenchmark.sh props_write.pg'))
        start_tpcc.setDaemon(True)
        start_tpcc.start()
        
        text = '-----step8:查看参数 track_activities; expect:参数都打开-----'
        sql = f"show track_activities;"
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('off', status, '执行失败' + text)

        text = '-----step9:查询gs_lwlock_status; expect:数据为0-----'
        self.log.info(text)
        sql = "select case when count(*) > 0 then 'yes' else 'no' end as have_info " + \
              "from gs_lwlock_status(); "
        time.sleep(20)
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('no', status, '执行失败：不应该查询得到lwlock锁的信息。' + text)
        

    def tearDown(self):
        text = '-----恢复环境为初始集群状态; expect:成功-----'
        self.log.info(text)
        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        status = Primary_SH.check_cluster_core_status(status, wait_times=600)
        self.log.info(status)
        text = '-----恢复参数 expect:关闭成功-----'
        result = Primary_SH.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'track_activities=on')
        self.assertTrue(result, '执行失败' + text)
        
        status = Primary_SH.exec_cm_ctl(mode='stop')
        self.log.info(status)
        self.assertIn('cm_ctl: stop cluster successfully.', status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='start')
        self.log.info(status)
        self.assertIn('cm_ctl: start cluster successfully.', status, '执行失败' + text)

        sql = f"show track_activities;"
        self.log.info(sql)
        status = Primary_SH.execut_db_sql(sql)
        self.log.info(status)
        self.assertIn('on', status, '参数未恢复' + text)
        
        status = Primary_SH.exec_cm_ctl(mode='switchover', param='-a')
        self.log.info(status)
        self.assertIn(self.constant.cm_switchover_success_msg, status, '执行失败' + text)

        status = Primary_SH.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
